package cj.web.admin.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.utils.Uploader;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.SourceEnum;
import cj.web.admin.dao.ContantDAO;
import cj.web.admin.domain.Contant;
import cj.web.login.controllers.BaseController;

@Path("/contant")
public class ContantController extends BaseController{

	private static Logger logger = Logger.getLogger(ContantController.class.getName());
	
	@Autowired
	private ContantDAO contantDao;
	
	@Get("show")
	public String show(Model model,Contant contant)
	{
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
		return "/views/portal/contantList.vm";
	}

	@Post("getList")
	public String getUserList(Contant contant) {
		
		JSONObject jo = new JSONObject();
		
		List<Contant> objs = contantDao.query(contant);
		
		int size = contantDao.querysize(contant);
		jo.put("data", objs);
		jo.put("draw", contant.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("queryById")
	public String queryById(Model model,@Param("id") String id)
	{
		contantDao.queryById(id);
		return null;
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
		return "/views/portal/contantDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Contant contant,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			
			contant.setId(ReturnUUID.getUUID());
			contant.setCreateUser(returnSessionObj(request).getAccount());
			contant.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
			contant.setUpdateUser(returnSessionObj(request).getAccount());
			contant.setContent(umContent);
			contant.setReleaseState(Constants.portal.releaseState.state_1);
			contant.setReleaseAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
			contantDao.create(contant);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") String id)
	{
		Contant obj = contantDao.queryById(id);
		// EnumSet的使用
        EnumSet<CategoryEnum> categoryEnum = EnumSet.allOf(CategoryEnum.class);
        EnumSet<SourceEnum> sourceEnum = EnumSet.allOf(SourceEnum.class);
        
        model.add("categoryEnum",categoryEnum);
        model.add("sourceEnum",sourceEnum);
		model.add("contant", obj);
		return "/views/portal/contantEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Contant contant,@Param("umContent") String umContent,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			contant.setContent(umContent);
			contant.setUpdateUser(returnSessionObj(request).getAccount());
			contant.setReleaseState(Constants.portal.releaseState.state_1);
			contant.setReleaseAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
			contantDao.update(contant);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") String id)
	{
		contantDao.deleteById(id);
		return "r:show";
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath") + "/upload/";
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", "/upload/"+rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/"+fileName+".thumb.jpg", 300, 300, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	@Get("fileDelete")
    public String fileDelete(@Param("key") String key) {
		JSONObject jo = new JSONObject();
		jo.put("key", key);
		jo.put("id", key);
		return "@"+jo.toString();
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") String id) {
		contantDao.updateByStickTime(id);
		return "r:show";
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") String id) {
		contantDao.updateByStickTimeIsNull(id);
		return "r:show";
	}
}
